/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csse3004.GD.PAdmin;

/*import csse3004.GD.DA.PollAdminDA;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.portlet.ModelAndView;
*/

/**
 *
 * @author Mamdouh Alharbi 41942989
 */
/*@Controller
public class PAhomeController {

    private PollAdminDA pollAdminService = new PollAdminDA();
    protected final Log logger = LogFactory.getLog(getClass());

    @RequestMapping(value= "home.html",method = RequestMethod.GET)
    public ModelAndView handleRequest(HttpServletRequest hsr, HttpSession session) {
        //Map<String, Object> myModel = new HashMap<String, Object>();
        if (hsr.getParameter("cid") != null || hsr.getParameter("pid") != null) {

            int cid = Integer.parseInt(hsr.getParameter("cid"));
            int pid = Integer.parseInt(hsr.getParameter("pid"));

            // PollAdminService.assignToPollCreator(pid, cid);

            logger.info("in home controller ");
            logger.info("cid " + cid);
            logger.info("pid " + pid);
            String remove = "Poll creator has been removed";
            pollAdminService.removePollCreator(pid, cid);
            return new ModelAndView("home", "remove", remove);
            //TODO @Mamdouhl  database stuff
        }

        ArrayList<HomeData> pollList = new ArrayList<HomeData>();
        //pollList = HomeData.genTempData(); 
        String userID = (String)session.getAttribute("userID");
        logger.info("userID:"+userID);
        if (userID != null) {
            pollList = pollAdminService.getHomeData(Integer.parseInt(userID));
            return new ModelAndView("home", "polls", pollList);
        }
        logger.info("returning error");
        String error = "There user isn't logged in";
        return new ModelAndView("home", "error", error);
    }
}
*/
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList;
import csse3004.GD.DA.PollAdminDA;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.mvc.Controller;
public class PAhomeController implements Controller {

    private PollAdminDA pollAdminService = new PollAdminDA();
    protected final Log logger = LogFactory.getLog(getClass());  
    public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) {
  
        if (hsr.getParameter("cid") != null || hsr.getParameter("pid") != null) {
            int cid = Integer.parseInt(hsr.getParameter("cid"));
            int pid = Integer.parseInt(hsr.getParameter("pid"));
            // PollAdminService.assignToPollCreator(pid, cid);
            logger.info("in home controller ");
            logger.info("cid " + cid);
            logger.info("pid " + pid);
            String remove = "Poll creator has been removed";
            pollAdminService.removePollCreator(pid, cid);
            return new ModelAndView("home", "remove", remove);
        }
        ArrayList<HomeData> pollList = new ArrayList<HomeData>();
        //pollList = HomeData.genTempData(); 
        try{
        int userID = Integer.parseInt((String) hsr.getSession().getAttribute("userID"));
        logger.info("userID:"+userID);
        logger.info("userID:"+userID);
        pollList = pollAdminService.getHomeData(userID);
        return new ModelAndView("home", "polls", pollList);    }
        catch (Exception e){
            String error = "There user isn't logged in";
            return new ModelAndView("home", "error", error);
        }
    }
}